-- drop function if exists sm_sc.fv_sgn_fft2(sm_sc.typ_l_complex[]);
create or replace function sm_sc.fv_sgn_fft2
(
  i_array          sm_sc.typ_l_complex[]
)
returns sm_sc.typ_l_complex[]
as
$$
-- declare
-- 如果要返回任意长度，要做循环卷积改造 https://blog.csdn.net/weixin_34128237/article/details/94637989

begin
  if array_ndims(i_array) = 2
  then
    return
    (
      with
      cte_fft_x as
      (
      select 
        array_agg(sm_sc.fv_sgn_fft(array(select unnest(i_array[a_no_y:a_no_y][:]))) order by a_no_y) as a_fft_x
      from generate_series(1, array_length(i_array, 1)) tb_a_y(a_no_y)    
      )
      select 
        |^~| array_agg(sm_sc.fv_sgn_fft(array(select unnest(a_fft_x[:][a_no_x:a_no_x]))) order by a_no_x) as a_fft_x
      from generate_series(1, power(2, ceil(log(2, array_length(i_array, 2)))) :: int) tb_a_x(a_no_x), cte_fft_x
    )
    ;
  else
    raise exception 'error dim!';
  end if;
end 
$$
language plpgsql stable
cost 100;

-- select sm_sc.fv_sgn_fft2
-- (
-- array
-- [ 
--   array[1,2,3,4,5,6,7,8,9,10,11,12],
--   array[2,3,4,5,6,7,8,9,10,11,12,13],
--   array[3,4,5,6,7,8,9,10,11,12,13,14],
--   array[4,5,6,7,8,9,10,11,12,13,14,15],
--   array[5,6,7,8,9,10,11,12,13,14,15,16]
-- ]
-- ):: sm_sc.typ_l_complex[]
-- -- 预期结果：
-- -- {{"(510.00000000,0.00000000)","(-213.86790834,42.36236838)","(27.92893270,108.63960980)","(59.57292973,-22.22567143)","(-30.00000030,-29.99999970)","(-19.36477665,40.70326143)","(42.07106810,18.63960880)","(13.65975367,-34.70870036)","(-30.00000000,0.00000000)","(13.65975574,34.70869942)","(42.07106730,-18.63960980)","(-19.36477713,-40.70326077)","(-29.99999970,29.99999970)","(59.57292925,22.22567077)","(27.92893190,-108.63960880)","(-213.86790627,-42.36236744)"}
-- -- ,{"(-64.97056272,246.24978312)","(-9.55192284,-119.58121247)","(-57.87005744,0.41421387)","(3.97292507,27.41999021)","(14.48528122,-14.48528150)","(-20.55154905,-13.86607894)","(-14.41421288,18.07106811)","(13.51325932,8.76414246)","(0.00000000,-14.48528136)","(-20.00442632,4.42688375)","(3.58578624,22.55634885)","(18.75499698,-4.83420387)","(-14.48528122,-14.48528122)","(-17.49008147,30.10871950)","(47.04162984,26.55634885)","(31.35679831,-86.94790847)"}
-- -- ,{"(102.00000000,-24.00000000)","(-36.74624217,12.49981315)","(10.41421364,19.72792194)","(12.41119167,-6.94174005)","(-6.00000006,-5.99999994)","(-2.20477670,7.80883090)","(9.24264068,1.72792174)","(1.93086304,-8.14065241)","(-6.00000000,0.00000000)","(3.53303877,5.74282753)","(7.58578636,-5.72792194)","(-5.54113403,-8.47247351)","(-5.99999994,5.99999994)","(11.41798010,1.94852842)","(0.75735932,-23.72792174)","(-48.80092068,-4.44513403)"}
-- -- ,{"(-31.02943728,42.24978312)","(1.69750642,-25.51010389)","(-11.58578637,-3.92893213)","(-1.38660977,4.29312486)","(2.48528134,-2.48528138)","(-3.80097817,-3.76100745)","(-4.12994221,2.41421365)","(1.32529725,2.16733191)","(0.00000000,-2.48528136)","(-4.42542845,0.09589044)","(-1.04163059,4.55634909)","(2.94295893,0.55254618)","(-2.48528134,-2.48528134)","(-5.06907948,5.57724122)","(6.41421341,8.55634907)","(8.71633327,-9.92469111)"}
-- -- ,{"(102.00000000,0.00000000)","(-42.77358166,8.47247368)","(5.58578654,21.72792196)","(11.91458595,-4.44513429)","(-6.00000006,-5.99999994)","(-3.87295533,8.14065229)","(8.41421362,3.72792176)","(2.73195073,-6.94174008)","(-6.00000000,0.00000000)","(2.73195114,6.94173988)","(8.41421346,-3.72792196)","(-3.87295543,-8.14065215)","(-5.99999994,5.99999994)","(11.91458585,4.44513415)","(5.58578638,-21.72792176)","(-42.77358125,-8.47247348)"}
-- -- ,{"(-31.02943728,-42.24978312)","(8.71633338,9.92469119)","(6.41421344,-8.55634919)","(-5.06907955,-5.57724125)","(-2.48528134,2.48528138)","(2.94295895,-0.55254622)","(-1.04163072,-4.55634911)","(-4.42542848,-0.09589018)","(0.00000000,2.48528136)","(1.32529714,-2.16733199)","(-4.12994224,-2.41421353)","(-3.80097810,3.76100747)","(2.48528134,2.48528134)","(-1.38660979,-4.29312482)","(-11.58578624,3.92893215)","(1.69750645,25.51010363)"}
-- -- ,{"(102.00000000,24.00000000)","(-48.80092115,4.44513419)","(0.75735944,23.72792198)","(11.41798021,-1.94852851)","(-6.00000006,-5.99999994)","(-5.54113396,8.47247366)","(7.58578656,5.72792178)","(3.53303844,-5.74282775)","(-6.00000000,0.00000000)","(1.93086351,8.14065225)","(9.24264056,-1.72792198)","(-2.20477681,-7.80883081)","(-5.99999994,5.99999994)","(12.41119160,6.94173990)","(10.41421344,-19.72792178)","(-36.74624184,-12.49981293)"}
-- -- ,{"(-64.97056272,-246.24978312)","(31.35679884,86.94790929)","(47.04163021,-26.55634935)","(-17.49008187,-30.10871970)","(-14.48528122,14.48528150)","(18.75499723,4.83420361)","(3.58578565,-22.55634913)","(-20.00442669,-4.42688255)","(0.00000000,14.48528136)","(13.51325879,-8.76414328)","(-14.41421325,-18.07106761)","(-20.55154865,13.86607914)","(14.48528122,14.48528122)","(3.97292482,-27.41998994)","(-57.87005685,-0.41421359)","(-9.55192247,119.58121127)"}}

